// d:\byte\byte_vue\智慧油价\pages\setting\setting.js
const model = require('../../public/model/env.js');
const url = model.api_pro_url();
const key = model.key();
const appid = model.appid();
const itemList = ["使用优惠券", "不使用优惠券"];

Page({
  data: {
    className: 'containerdark',
    data1: '',
    data2: '',
    data3: '',
    resbtn: '查看结果',
    url: 'order',
    resbtn2: '查看结果',
    _value: '',
    number: 0,
    buttonDisabled: true,
    quan: 0,
    coupon: 0,
    coupon_used: false,
    // showdoudi: false,
  },
  one(e) {
    this.setData({
      _value: e.detail.value, // 绑定one字段
      number: e.detail.value.length, // 绑定number1字段
    });
  },


  onLoad(options) {
    const outerOptions = {
      value1: options.value1 || '',
      value2: options.value2 || '',
      value3: options.value3 || ''
    };
    const that = this; // 保存 Page 对象的引用
    const requestUrlquan = `${url}/index.php/api/points_cx.php`;
    const quans = {
      url: requestUrlquan,
      data: {
        // appid: appid,
        key: key,
        user_id: tt.getStorageSync('userid'),
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(quans);
    tt.request({
      ...quans,
      success: (res) => {
        const data = res.data;

        if (data.code == 200) {
          console.log(data.data);
          this.setData({
            coupon: data.data.ticket,
            // btnup: '立即使用',
          });
        }

      },
      fail: (err) => {
        console.log(err);
      }
    });


    // 查询用户是否有可用优惠券
    const datainput = tt.getStorageSync('datainput');
    console.log(datainput, options);
    // 合并获取值的逻辑，减少变量声明
    const value1 = datainput ? datainput.value1 : options.value1;
    const value2 = datainput ? datainput.value2 : options.value2;
    const value3 = datainput ? datainput.value3 : options.value3;
    tt.showToast({ title: '数据正在请求中', icon: 'none', duration: 1000 });

    // console.log(value1, value2, value3);
    tt.request({
      url: `${url}/index.php/api/gua`,
      data: { appid, key, input1: value1, input2: value2, input3: value3 },
      method: 'POST',
      header: { 'content-type': 'application/json' },
      dataType: 'json',

      success: (res) => {
        if (res.data.code === 207) {
          tt.showToast({ title: '请求结果异常', icon: 'none', duration: 2000 });
          return;
        }
        console.log('完整响应:', res.data);
        if (!res.data || !Array.isArray(res.data.data) || res.data.data.length === 0) {
          tt.showToast({ title: '重新请求失败', icon: 'none', duration: 2000 });
          tt.showModal({
            title: "网络卡顿",
            content: "需要重新请求数据...",
            showCancel: false,
            success(res) {
              if (res.confirm) {
                tt.showToast({
                  title: '稍等'
                });
                that.zaiciget(outerOptions); //
              }
            },
          });
          return;
        }

   

        const data = res.data.data[0];
        // console.log(data.gua_guyuanwen, res.data.data[0].gua_baihua);

        tt.setStorageSync('resgua', res.data);
        console.log('名称:', res.data.name);
        const data1 = `第 ${res.data.guanumber} 卦 , ${res.data.name} 。${res.data.yaobian} 爻 。 ${res.data.data[0].gua_guyuanwen}${res.data.symbol}`;
        tt.setStorageSync('resdata', data1);

        console.log('guyuanwen:' + data1);
        this.setData({

          data1: data1,
          data2: data.gua_baihua,
          data3: data.gua_tuanci,
          buttonDisabled: false,


        });



        // tt.showToast({ title: '卦象请求成功', icon: 'success', duration: 2000 });
      },
      fail: (err) => {
        console.error('请求卦象失败', err);
        tt.showToast({ title: '请求失败', icon: 'none', duration: 2000 });
        this.setData({ buttonDisabled: false });
      },

    });




  },
  zaiciget(params) {
    const { value1, value2, value3 } = params;
    const requestUrl = `${url}/index.php/api/gua`;

    const options = {
      url: requestUrl,
      data: { appid, key, input1: value1, input2: value2, input3: value3 },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    // console.log(options);
    tt.request({
      ...options,

      success: (res) => {
        if (res.data.code !== 200) {
          tt.showToast({ title: '请求结果异常', icon: 'none', duration: 2000 });
          return;
        }
        console.log('完整响应:', res.data);
        const data = res.data.data[0];
        console.log(data.gua_guyuanwen, res.data.data[0].gua_baihua);
        tt.setStorageSync('resgua', res.data);

        const data1 = `第 ${res.data.guanumber} 卦, ${res.data.yaobian} 爻: ${res.data.data[0].gua_guyuanwen}${res.data.symbol}`;
        tt.setStorageSync('resdata', data1);

        this.setData({
          data1: data1,
          data2: data.gua_baihua,
          data3: data.gua_tuanci,
          // buttonDisabled: false,
        });
      },
      fail: (err) => {
        console.error('请求卦象失败', err);
        tt.showToast({ title: '请求失败', icon: 'none', duration: 2000 });
        this.setData({ buttonDisabled: false });
      },
    });
  },
  youhuiquan() {
    let that = this;
    tt.showActionSheet({
      itemList,
      itemColor: "#968759",
      success(res) {
        console.log('showActionSheet success', res);
        if (itemList[res.tapIndex] == "使用优惠券") {
          that.setData({
            quan: 701,
            coupon_used: true,
          });
          tt.showToast({
            title: `点了${itemList[res.tapIndex]}`,
            icon: 'none'
          });
        } else {
          that.setData({
            quan: 0,
            coupon_used: false,
          });
          tt.showToast({
            title: `点了${itemList[res.tapIndex]}`,
            icon: 'none'
          });
        }
      },
      fail(err) {
        console.log('showActionSheet fail', err);
      },
      complete(res) {
        console.log('showActionSheet complete', res)
      }
    });
  },

  // 获取用户信息
  result() {
    this.setData({
      buttonDisabled: true
    });

    if (this.data._value.length > 5) {
      console.log(this.data._value);
      tt.setStorageSync('gua_content', this.data._value);
      tt.navigateTo({
        url: '../order/order?quan=' + this.data.quan,
      });
      this.setData({
        buttonDisabled: false
      });

    }
    else {

      tt.showToast({
        title: '请填写占卜事情',
        icon: 'none',
        duration: 2000
      });
      this.setData({
        buttonDisabled: false
      });
    }

  }

})